//
//  CYTabBar.swift
//  WeiBo
//
//  Created by Mr_J on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class CYTabBar: UITabBar {

  
    var composeButtonClickClosure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        backgroundImage = UIImage(named: "tabbar_background")
        
        addSubview(composeButton)
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let itemW = self.frame.width/5
        
        var index = 0
        
        for value in self.subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                let x = CGFloat(index) * itemW
                
                value.frame.size.width = itemW
                value.frame.origin.x = x
                
                index++
                
                if index == 2 {
                    index++
                }
                
            }
            
        }
        
        
        composeButton.center = CGPoint(x: self.center.x, y: self.frame.size.height * 0.5)
        
    }
    
    
    func composeButtonClick(){
        
        composeButtonClickClosure?()
    }
    
    
    private lazy var composeButton:UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents:.TouchUpInside)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
                print(button)
        return button
        

    }()
    

}
